<?php
class contact extends baseController {    
	public function __construct() {
		parent::__construct ();
	}
    
    public function loadView($temp) {
        if (!isset($temp["data"]))
            $temp["data"] = '';
        $this->load->view("template", $temp);
    }
    
    public function index() {        
        $this->session->set_userdata('selectedMenu', 'contact');
        $this->_data["template"] = 'contact';
        if(!$this->isPost()) {
            $this->loadView($this->_data);
            return;
        }
        
        $data = $this->input->post();
        $data['createDate'] = date('Y-m-d H:i:s');
        $this->contactModel->insert($data);
        
        // send mail
        $this->sendEmail($data);
        
        redirect(base_url().'contact/success');
    }
    
    public function success() {
        $this->_data["template"] = 'success';
        $this->loadView($this->_data);
    }
    
    public function sendEmail($data) {
        $message  = "<h4>This is a email contact from website of customer</h4><br/>";
        $message .= "<table><tr><td>Fullname : </td><td>".$data['fullName']."</td></tr>";
        $message .= "<tr><td>Email    : </td><td>".$data['email']."</td></tr>";
        $message .= "<tr><td>Phone    : </td><td>".$data['phone']."</td></tr>";
        $message .= "<tr><td>City     : </td><td>".$data['city']."</td></tr>";
        $message .= "<tr><td>Country  : </td><td>".$data['country']."</td></tr>";
        $message .= "<tr><td>Message  : </td><td>".$data['message']."</td></tr></table>";
        
        $socials = $this->socialModel->read(1);
        
        $mail = array(
            "name"          => 'Cypher Systems',
            "subject"       => 'Contact email from '.$data['fullName'],
            "to_receiver"   => $socials['email'],
            "message"       => $message,
        );

        $this->load->library("email");
        $this->email->sendmail($mail);
    }
}
?>